/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 * Description: 性能评估器相关源文件
 * Author: qingjicheng
 * Create: 2022-8-1
 */

#include "connected_subgraph_factory.h"
#include "evaluator_log_def.h"
#include "connected_subgraph/subgraph_ring.h"
#include "connected_subgraph/subgraph_mesh.h"
#include "connected_subgraph/subgraph_mutiring.h"
#include "connected_subgraph/subgraph_star.h"

namespace HcclEvaluator {
ConnectedSubgraphFactory::ConnectedSubgraphFactory()
{
}

ConnectedSubgraphFactory::~ConnectedSubgraphFactory()
{
}

HcclResult ConnectedSubgraphFactory::CreateSubgraphs(AdjacencyMatrix<> &matrix,
    std::vector<std::unique_ptr<ConnectedSubgraph>> &subgraphs)
{
    std::vector<AdjacencyMatrix<>> matrixes;
    EVALUATOR_CHK_RET(detector_.Detect(matrix, matrixes));
    if (matrixes.size() == 0) {
        EVALUATOR_ERROR("detect matrixes size is zero\n");
        return HCCL_E_NOT_SUPPORT;
    }

    for (auto &m : matrixes) {
        std::unique_ptr<ConnectedSubgraph> &&subgraphPtr = CreateSubgraphByMatrix(std::move(m));
        EVALUATOR_CHK_PTR_NULL(subgraphPtr);
        subgraphs.push_back(std::forward<std::unique_ptr<ConnectedSubgraph> &&>(subgraphPtr));
    }
    return HCCL_SUCCESS;
}

std::unique_ptr<ConnectedSubgraph> ConnectedSubgraphFactory::CreateSubgraphByMatrix(AdjacencyMatrix<> &&matrix)
{
    switch (matrix.GetAttr()) {
        case AdjaMatrixAttr::NOT_DETECT:
            EVALUATOR_ERROR("adja matrix did not be detected\n");
            return nullptr;
        case AdjaMatrixAttr::RING:
            return std::unique_ptr<ConnectedSubgraph>(new (std::nothrow)
                SubgraphRing(std::forward<AdjacencyMatrix<> &&>(matrix)));
        case AdjaMatrixAttr::FULL_MESH:
            return std::unique_ptr<ConnectedSubgraph>(new (std::nothrow)
                SubgraphMesh(std::forward<AdjacencyMatrix<> &&>(matrix)));
        case AdjaMatrixAttr::MULTI_RING:
            return std::unique_ptr<ConnectedSubgraph>(new (std::nothrow)
                SubgraphMultiRing(std::forward<AdjacencyMatrix<> &&>(matrix)));
        case AdjaMatrixAttr::STAR:
            return std::unique_ptr<ConnectedSubgraph>(new (std::nothrow)
                SubgraphStar(std::forward<AdjacencyMatrix<> &&>(matrix)));
        default:
            EVALUATOR_ERROR("adja matrix attr error\n");
            return nullptr;
    }
}
} // namespace HcclEvaluator